package edu.uestc.gene.entity;

import edu.uestc.gene.model.Statistics;
import edu.uestc.gene.model.Tool;

/**
 * 
 * 全局基因副作用关系表
 * <table border="1" cellspacing="0" align="left" cellpadding="0" bordercolor=#000000>
 * <tr>
 * <td></td>
 * <td>a1</td>
 * <td>a2</td>
 * <td>a3</td>
 * <td>a4</td>
 * </tr>
 * <tr>
 * <td>g1</td>
 * <td></td>
 * <td>d3</td>
 * <td></td>
 * <td>d3</td>
 * </tr>
 * <tr>
 * <td>g2</td>
 * <td>d1</td>
 * <td></td>
 * <td>d3</td>
 * <td></td>
 * </tr>
 * <tr>
 * <td>g3</td>
 * <td></td>
 * <td>d3</td>
 * <td>d2</td>
 * <td>d3</td>
 * </tr>
 * <tr>
 * <td>g4</td>
 * <td>d1</td>
 * <td>d3</td>
 * <td>d1</td>
 * <td>d3</td>
 * </tr>
 * </table>
 * <br>
 * 
 * 
 * @author Carl
 * 
 *         2013-10-29
 */
public class RelationshipTable {

	private static final String NULL = "NULL";// 位置空，全局表初始化为全NULL值

	private String[][] relationshipTable;// 全局药物表
	private int[][] relationshipCounterTable;// 全局药物表对应数量

	private static RelationshipTable table;

	private int[] gene_drugs;

	private long all = 0L;

	public static synchronized RelationshipTable getInstance() {
		if (table == null) {
			table = new RelationshipTable();
		}
		return table;
	}

	private RelationshipTable() {
		relationshipTable = new String[Statistics.GENES_NUM][Statistics.ADRS_NUM];
		relationshipCounterTable = new int[Statistics.GENES_NUM][Statistics.ADRS_NUM];
		for (int i = 0; i < Statistics.GENES_NUM; i++) {
			for (int j = 0; j < Statistics.ADRS_NUM; j++) {
				relationshipTable[i][j] = NULL;
				relationshipCounterTable[i][j] = 0;
			}
		}
		gene_drugs = new int[Statistics.GENES_NUM];
	}

	/**
	 * 根据编码的基因和ADR添加药物
	 * 
	 * @param gene
	 * @param adr
	 * @param drug
	 */
	public void addRelationship(int gene, int adr, int drug) {
		if (relationshipTable[gene][adr].equals(NULL)) {
			relationshipTable[gene][adr] = drug + "";
		} else {
			relationshipTable[gene][adr] += ("," + drug);
		}
	}

	private int getRelationshipDrugs(int gene, int adr) {
		return relationshipTable[gene][adr].split(",").length;
	}

	public double getProbabilityOfGeneByADR(int gene, int adr) {
		return Tool.roundDivision(relationshipCounterTable[gene][adr],
				gene_drugs[gene], Statistics.ROUND_SCALE);
	}

	public double getProbabilityOfGenesByADR(int[] genes, int adr) {
		double[] probs = new double[genes.length];
		for (int i = 0; i < genes.length; i++) {
			probs[i] = getProbabilityOfGeneByADR(genes[i], adr);
		}
		return Tool.getIntersectionOfProbability(probs);
	}

	public void index() {
		for (int i = 0; i < gene_drugs.length; i++) {
			int count = 0;
			for (int j = 0; j < relationshipTable[i].length; j++) {
				int num = getRelationshipDrugs(i, j);
				if (relationshipTable[i][j].equals("NULL")) {
					num = 0;
				}
				relationshipCounterTable[i][j] = num;
				count += num;
			}
			gene_drugs[i] = count;
			all += count;
		}
		// System.out.println(all);
	}

	public long getAll() {
		return all;
	}

	/**
	 * 根据编码的基因和ADR获得药物
	 * 
	 * @param gene
	 * @param adr
	 * @param drug
	 */
	public String getRelationship(int gene, int adr) {
		return relationshipTable[gene][adr];
	}
}
